﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using MediaPortal.GUI.Library;
using System.Xml;
using System.Xml.XPath;
using System.IO;
//using MediaPortal.Dialogs;

namespace MediaPortal.Plugins
{

    public class iPlayerService : IPlugin, ISetupForm
    {
        #region Variables

        Preferences prefs = new Preferences();

        #endregion Variables

        public iPlayerService()
        {
            
        }

        #region IPlugin methods

                /// <summary>
                /// Starts this instance.
                /// </summary>
                public void Start()
                {
                    Log.Debug("iPlayerService: Starting");
                    updateList();
                    

                }

                /// <summary>
                /// Stops this instance.
                /// </summary>
                public void Stop()
                {


                  Log.Debug("iPlayerService: Stopped");
                }
        
        #endregion IPlugin methods
                

        #region ISetupForm Members

        // Returns the name of the plugin which is shown in the plugin menu
        public string PluginName()
        {
            return "iPlayerService";
        }

        // Returns the description of the plugin is shown in the plugin menu
        public string Description()
        {
            return "Plugin for iPlayer.";
        }

        // Returns the author of the plugin which is shown in the plugin menu
        public string Author()
        {
            return "Happyf1sh";
        }

        // show the setup dialog
        public void ShowPlugin()
        {
            ConfigForm configForm = new ConfigForm();
            configForm.ShowDialog();
        }

        // Indicates whether plugin can be enabled/disabled
        public bool CanEnable()
        {
            return true;
        }

        // Get Windows-ID
        public int GetWindowId()
        {
            // WindowID of windowplugin belonging to this setup
            // enter your own unique code
            return 0;
        }

        // Indicates if plugin is enabled by default;
        public bool DefaultEnabled()
        {
            return true;
        }

        // indicates if a plugin has it's own setup screen
        public bool HasSetup()
        {
            return true;
        }

        /// <summary>
        /// If the plugin should have it's own button on the main menu of MediaPortal then it
        /// should return true to this method, otherwise if it should not be on home
        /// it should return false
        /// </summary>
        /// <param name="strButtonText">text the button should have</param>
        /// <param name="strButtonImage">image for the button, or empty for default</param>
        /// <param name="strButtonImageFocus">image for the button, or empty for default</param>
        /// <param name="strPictureImage">subpicture for the button or empty for none</param>
        /// <returns>true : plugin needs it's own button on home
        /// false : plugin does not need it's own button on home</returns>

        public bool GetHome(out string strButtonText, out string strButtonImage,
          out string strButtonImageFocus, out string strPictureImage)
        {
            strButtonText = strButtonImage =  strButtonImageFocus = strPictureImage = String.Empty;
            return false;
        }

        #endregion


        #region plugin methods

        /// <summary>
        /// Go online and update the program list
        /// </summary>
        private void updateList()
        {
            try
            {
                prefs.loadPreferences("");

                if (prefs.logFile != "")
                {
                    if (File.Exists(prefs.logFile))
                    {
                        FileInfo fInfo = new FileInfo(prefs.logFile);
                        if (fInfo.Length > 1000000)
                        {
                            File.Delete(prefs.logFile);
                        }
                    }

                }

                LogToFile("Running Cache Service");
                TVProgramList myList = new TVProgramList(prefs.cacheLocation);
                myList.LoadFromFile();

                if (prefs.getBBC == "1")
                {
                    try
                    {
                        LogToFile("Loading BBC");
                        iPlayerTranslator p1 = new iPlayerTranslator(prefs);
                        myList = p1.loadDatabase(myList, "0");
                        LogToFile("added " + p1.added + ", removed " + p1.removed + ", total " + p1.total);
                    }
                    catch (Exception e1)
                    {
                        LogToFile("Loading Failed with error" + e1.ToString());
                    }

                }
                //if (prefs.getITV == "1")
                //{
                //    try
                //    {
                //        LogToFile("Loading ITV");
                //        ITVTranslator itv = new ITVTranslator(prefs);
                //        myList = itv.loadDatabase(myList, "1");
                //        LogToFile( "added " + itv.added + ", removed " + itv.removed + ", total " + itv.total);
                //    }
                //    catch (Exception e1)
                //    {
                //        LogToFile("Loading Failed with error" + e1.ToString());
                //    }

                //}
                //if (prefs.getC4 == "1")
                //{
                //    try
                //    {
                //        LogToFile("Loading C4");

                //        C4Translator c4 = new C4Translator(prefs);
                //        myList = c4.loadDatabase(myList, "1");
                //        LogToFile("added " + c4.added + ", removed " + c4.removed + ", total " + c4.total);
                //    }
                //    catch (Exception e1)
                //    {
                //        LogToFile("Loading Failed with error" + e1.ToString());
                //    }
                //}
                //if (prefs.getFive == "1")
                //{
                //    try
                //    {
                //        LogToFile("Loading Five");

                //        FiveTranslator Five = new FiveTranslator(prefs);
                //        myList = Five.loadDatabase(myList, "1");
                //        LogToFile("added " + Five.added + ", removed " + Five.removed + ", total " + Five.total);
                //    }
                //    catch (Exception e1)
                //    {
                //        LogToFile("Loading Failed with error" + e1.ToString());
                //    }
                //}
                LogToFile("Writing to " + prefs.cacheLocation);

                myList.WriteToFile();

                prefs.getLatestResolutions();

            }
            catch (Exception e2)
            {
                LogToFile(e2.ToString());
            }
        }

        private void LogToFile(string contents)
        {
            if (prefs.logFile != "")
            {
                //set up a filestream
                FileStream fs = new FileStream(prefs.logFile, FileMode.OpenOrCreate, FileAccess.Write);

                //set up a streamwriter for adding text

                StreamWriter sw = new StreamWriter(fs);

                //find the end of the underlying filestream

                sw.BaseStream.Seek(0, SeekOrigin.End);

                //add the text
                sw.WriteLine(contents);
                //add the text to the underlying filestream

                sw.Flush();
                //close the writer
                sw.Close();
            }
        }

        #endregion plugin methods

    }


}
